﻿var gbFirstQuery = true;
var pageRows = 17;
var totalPages = 0;
var nCurPage = 1;
var selectAlbumsId = null;
var flashObj = null;
$(function() {
	var req = GetRequest();
	selectAlbumsId = req["id"];
	f_getData();
});

function f_getData() {
	var _nCurPage = 0;
	if (gbFirstQuery) {
		_nCurPage = nCurPage * (-1);
	} else {
		_nCurPage = nCurPage;
	}

	var gcData = new AjaxProxy();
	gcData.addParm("nXcbh", selectAlbumsId);
	gcData.addParm("currPage", _nCurPage);
	gcData.addParm("pageRows", pageRows);
	gcData.invoke("albums.getPageContent", false);
	var count = gcData.getRowCount("P_RESULT");
	if (gbFirstQuery) {
		// var totalRows = gcData.getString("totalRows");
		totalPages = gcData.getString("totalPages");
		$("#_totalPages").html(totalPages);
		$("#_currPage").html(nCurPage);
		$("#_totalPage").html(totalPages);
		if (totalPages > 1) {
			$("#_first").html("首页");
			$("#_back").html("上一页");
			$("#_next").html(
					"<a href='javascript:void(0)' onclick='f_next()'>下一页</a>");
			$("#_last").html(
					"<a href='javascript:void(0)' onclick='f_last()'>尾页</a>");
		} else {
			$("#_first").html("首页");
			$("#_back").html("上一页");
			$("#_next").html("下一页");
			$("#_last").html("尾页");
		}
		gbFirstQuery = false;
	} else {
		if (_nCurPage == totalPages) {
			$("#_first").html(
					"<a href='javascript:void(0)' onclick='f_first()'>首页</a>");
			$("#_back").html(
					"<a href='javascript:void(0)' onclick='f_back()'>上一页</a>");
			$("#_next").html("下一页");
			$("#_last").html("尾页");
		} else if (_nCurPage == 1) {
			$("#_first").html("首页");
			$("#_back").html("上一页");
			$("#_next").html(
					"<a href='javascript:void(0)' onclick='f_next()'>下一页</a>");
			$("#_last").html(
					"<a href='javascript:void(0)' onclick='f_last()'>尾页</a>");
		} else {
			$("#_first").html(
					"<a href='javascript:void(0)' onclick='f_first()'>首页</a>");
			$("#_back").html(
					"<a href='javascript:void(0)' onclick='f_back()'>上一页</a>");
			$("#_next").html(
					"<a href='javascript:void(0)' onclick='f_next()'>下一页</a>");
			$("#_last").html(
					"<a href='javascript:void(0)' onclick='f_last()'>尾页</a>");
		}
	}
	f_ShowData(gcData);
}

function f_ShowData(data) {
	var count = data.getRowCount("P_RESULT");
	$("#tbodyImage").children().remove();
	for ( var i = 0; i < count; i++) {
		var nId = data.getString("P_RESULT", i, "N_XH");
		var mc = data.getString("P_RESULT", i, "C_MC");
		var size = data.getString("P_RESULT", i, "N_SIZE");
		var zt = data.getString("P_RESULT", i, "C_ZT");
		var _zt = zt == 0 ? "<font color='#ff0000'>未通过</font>"
						: "<font color='#000000'>正常</font>";
		// var path = data.getString("P_RESULT",i,"cPath");
		var str = "<tr value='" + nId + "'>";
		str += "<td style='height:26px;border-bottom:1px #f0f0f0 dotted;text-overflow:ellipsis;white-space:nowrap;overflow:hidden;'>"
				+ mc + "</td>";
		str += "<td style='border-bottom:1px #f0f0f0 dotted; width:60px;' align='center'>"
				+ ((size / 1024).toFixed(2)) + "&nbsp;MB</td>";
		if (zt == 0) {
			str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'><img src='../../images/u.png' style='filter:alpha(opacity=20)'></td>";
		} else {
			str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'><img src='../../images/u.png' title='修改' onClick='imgUpdate(this)' onmouseover='this.style.cursor=\"pointer\"'></td>";
		}
		str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'><img src='../../images/x.png' title='删除' onClick='imgDeletes(this)' onmouseover='this.style.cursor=\"pointer\"'></td>";
		str += "<td style='border-bottom:1px #f0f0f0 dotted; width:60px;' align='center'>"
				+ _zt + "</td>";
		if (zt == "0") {// 未通过
			str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'><img src='../../images/search.png' style='filter:alpha(opacity=20)'></td>";
			str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'><img src='../../images/copy.gif' style='filter:alpha(opacity=20)'></td>";
		} else if (zt == "1") {
			str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'>";
			str += "<a href='"
					+ imgPreview(nId)
					+ "' rel='imagePreview' title='"+mc+"''><img src='../../images/search.png' title='预览图片'></a>";
			str += "</td>";
			
			//str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' align='center'><img src='../../images/copy.gif' title='复制链接' onClick='imgCopy(this)' onmouseover='this.style.cursor=\"pointer\"'></td>";
			str += "<td style='border-bottom:1px #f0f0f0 dotted; width:50px;' title='复制图片地址' align='center' id='d_clip_"+nId+"' class='my_clip_button'><img src='css/copy.gif'></td>";
		} else {
			//copyHtml("flash_"+nId,16,16,imgCopy2(nId))
		}
		str += "</tr>";

		$("#tbodyImage").append(str);
		if(zt == "1") {
			var clip = new ZeroClipboard.Client('d_clip_'+nId,nId);
			clip.setHandCursor( true );
			
			clip.addEventListener('mousedown', function (client) {
				client.setText( imgCopy2(client.nId) );
			});
			
			clip.addEventListener('complete', function (client, text) {
				alert("提示：链接地址已复制!");
			});
		}
		
	}
	$("a[rel='imagePreview']").colorbox({
		slideshow :true,
		maxWidth:650,
		maxHeight:700
	});
	initGrid();
}

function imgCopy2(nXh) {
	var preview = new AjaxProxy();
	preview.addParm("nXh", nXh);
	preview.invoke("albums.getImagPath", false);
	var flag = preview.getString("PN_RET");
	if (flag != 0) {
		alert(preview.getString("PC_MSG"));
	} else {
		return preview.getString("path");
	}
}

function imgCopy(obj) {
	var nXh = $(obj).parent().parent().attr("value");
	var preview = new AjaxProxy();
	preview.addParm("nXh", nXh);
	preview.invoke("albums.getImagPath", false);
	var flag = preview.getString("PN_RET");
	if (flag != 0) {
		alert(preview.getString("PC_MSG"));
	} else {
		window.clipboardData.setData("Text", preview.getString("path"));
	}
}

function imgUpdate(obj) {
	var $this = $(obj);
	var aValue = $this.parent().parent().children().eq(0).html();
	if($(aValue).attr("type")){
		return;
	}
	var $input = $("<input type='text' maxLength='32' style='border:0px; background-color:#d0dafd; height:18px; border-bottom:1px solid #000; width:150px;' value='"
			+ aValue + "'>");
	$this.parent().parent().children().eq(0).html("");
	$this.parent().parent().children().eq(0).append($input);
	$input.focus();
	$input.bind("blur", function() {
		if ($.trim($(this).val()) == "") {
			$this.parent().parent().children().eq(0).html(aValue);
		} else {
			if ($(this).val() == aValue) {
				$this.parent().parent().children().eq(0).html($(this).val());
				return;
			}
			var nXh = $(obj).parent().parent().attr("value");
			var upd = new AjaxProxy();
			upd.addParm("nXh", nXh);
			upd.addParm("cMc", $(this).val());
			upd.invoke("albums.updUserImage", false);
			var flag = upd.getString("PN_RET");
			if (flag != 0) {
				$this.parent().children().eq(0).html(aValue);
				alert(upd.getString("PC_MSG"));
			} else {
				$this.parent().parent().children().eq(0).html($(this).val());
				alert("图片名字修改成功!");
			}
		}
	});
}

function imgDeletes(obj) {
	if (!confirm("确定要删除当前图片吗？\r\n删除之后将不能恢复！"))
		return;
	var nXh = $(obj).parent().parent().attr("value");
	var del = new AjaxProxy();
	del.addParm("nXh", nXh);
	del.invoke("albums.deleteUserImage", false);
	var flag = del.getString("PN_RET");
	if (flag != 0) {
		alert(del.getString("PC_MSG"));
	}
	totalPages = 0;
	nCurPage = 1;
	gbFirstQuery = true;
	f_getData();
	alert("已删除！");
}

function reloadData() {
	totalPages = 0;
	nCurPage = 1;
	gbFirstQuery = true;
	f_getData();
}

function f_first() {
	nCurPage = 1;
	$("#_currPage").html(nCurPage);
	f_getData();
}

function f_back() {
	nCurPage--;
	$("#_currPage").html(nCurPage);
	f_getData();
}

function f_next() {
	nCurPage++;
	$("#_currPage").html(nCurPage);
	f_getData();
}

function f_last() {
	nCurPage = totalPages;
	$("#_currPage").html(nCurPage);
	f_getData();
}

function imgPreview(nXh) {
	var preview = new AjaxProxy();
	preview.addParm("nXh", nXh);
	preview.invoke("albums.getImagPath", false);
	var flag = preview.getString("PN_RET");
	if (flag != 0) {
		alert(preview.getString("PC_MSG"));
		return "";
	} else {
		return preview.getString("path");
	}
}

function initGrid() {
	$(".newstable tbody tr").hover(function() {
		$(this).css({
			background : "#d0dafd",
			color : "339"
		});
	}, function() {
		$(this).css({
			background : "#fcfcfc"
		});
	});
}

function GetRequest() {
	var url = location.search; // 获取url中"?"符后的字串
	var theRequest = new Object();
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for ( var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}

function copyHtml(id,width,height,url) {
	// return HTML for movie
	var html = '';
	var flashvars = 'url=' + url;
		
	if (navigator.userAgent.match(/MSIE/)) {
		// IE gets an OBJECT tag
		var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
		html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="'+id+'" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="css/copyUrl.swf" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><param name="wmode" value="transparent"/></object>';
	}
	else {
		// all other browsers get an EMBED tag
		html += '<embed id="'+id+'" src="css/copyUrl.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+id+'" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />';
	}
	return html;
}
